<%@ page language="java" import="java.util.*" import="java.sql.*" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="com.example.demo2.DBUtil" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
</head>
<link rel="stylesheet" href="css/bootstrap.css">
<body>
<style>
    .center{display: flex !important;align-content: center;justify-content: center;align-items: center;}
</style>
<div style="display: flex; justify-content: center;flex-direction: column;align-items: center;">
    <%@include file="admin_header.jsp"%>
    <div style="margin-top: 20px">
        <div style="display: flex;justify-content: space-between;">
            <button type="button" class="btn btn-primary" style="margin-bottom: 20px" data-toggle="modal" data-target="#exampleModal">添加用户</button>
            <a href="ad_user.jsp"><button type="button" class="btn btn-success" style="margin-bottom: 20px;">刷新</button></a>
        </div>
         <ul class="list-group list-group-horizontal">
            <li class="list-group-item" style="width: 150px">ID</li>
            <li class="list-group-item"style="width: 150px">用户名</li>
            <li class="list-group-item"style="width: 150px">密码</li>
            <li class="list-group-item"style="width: 150px">地址</li>
            <li class="list-group-item"style="width: 150px">操作</li>
        </ul>


        <%
            request.setCharacterEncoding("UTF-8");
            String up_address = request.getParameter("up_address");
            String a_address = request.getParameter("address");
            String user = request.getParameter("user");
            String up_id = request.getParameter("up_id");
            String up_psw = request.getParameter("up_psw");
            String a_psw = request.getParameter("psw");
            String d_id = request.getParameter("d_id");
            Connection cn = null;
            PreparedStatement ps = null;

            ResultSet rs = null;
            String sql = "";
            cn = DBUtil.getConnection();
            sql = "select * from user";
            ps = cn.prepareStatement(sql);
            rs = ps.executeQuery();



            String error = "";
            if (user != null && a_psw !=null){
                sql = "select * from user where user = ?";
                ps = cn.prepareStatement(sql);
                ps.setString(1, user);
                rs = ps.executeQuery();
                if (rs.next()) {
//                    System.out.println("已存在");
                    error ="已存在";
                    out.println("<script>\n" +
                            "    alert(\"添加失败,用户名已存在!\")\n" +
                            "</script>");
                }else{
                    int n=0;
                    sql = "insert into user(user,psw,address) values(?,?,?)";
                    ps = cn.prepareStatement(sql);
                    ps.setObject(1, user);
                    ps.setObject(2, a_psw);
                    ps.setObject(3, a_address);
                    n= ps.executeUpdate();
                    response.sendRedirect("ad_user.jsp");

                }
            }
            if (up_id != null){
                int n=0;
                sql = "UPDATE user SET psw =\""+up_psw+"\", address =\""+up_address +"\" WHERE id=" + up_id;
                System.out.println(sql);
                ps = cn.prepareStatement(sql);
                n= ps.executeUpdate();
                response.sendRedirect("ad_user.jsp");

            }

            if (d_id != null){
              int n=0;
              sql = "DELETE FROM `gp`.`user` WHERE `id` = " + d_id;
//              System.out.println(sql);
              ps = cn.prepareStatement(sql);
              n= ps.executeUpdate();
              response.sendRedirect("ad_user.jsp");
            }
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("user");
                String psw = rs.getString("psw");
                String address = rs.getString("address");
        %>
        <ul class="list-group list-group-horizontal-sm">
            <li class="list-group-item center"style="width: 150px;"><%=id%></li>
            <li class="list-group-item center"style="width: 150px"><%=name%></li>
            <li class="list-group-item center"style="width: 150px"><%=psw%></li>
            <li class="list-group-item center"style="width: 150px"><%=address%></li>
            <li class="list-group-item center"style="width: 150px">
                <button type="button" class="btn btn-primary btn-sm"
                        data-toggle="modal" data-target="#upModal<%=id%>"
                        style="margin-bottom: 20px;margin-right: 5px">修改</button>
                <a href="ad_user.jsp?d_id=<%=id%>" type="button" class="btn btn-info btn-sm" style="margin-bottom: 20px">删除</a>
            </li>
        </ul>
        <%@include file="up_user.jsp"%>
        <%
            }
        %>
    </div>


    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">添加用户</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="ad_user.jsp" method="post">
                        <div class="form-group">
                            <label for="user">用户名:</label>
                            <input type="text" name="user" required class="form-control" id="user">
                        </div>
                        <div class="form-group">
                            <label for="password">密码:</label>
                            <input type="password" name="psw" required class="form-control"
                                   id="password">
                        </div>
                        <div class="form-group">
                            <label for="address">地址:</label>
                            <input type="text" name="address" required class="form-control"
                                   id="address">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                            <button type="submit" class="btn btn-primary">确认</button>
                        </div>
                    </form>
                </div>

            </div>
        </div>
    </div>

</div>
<script src="js/jQuery.js"></script>
<script src="js/bootstrap.js"></script>

</body>
</html>